package com.techysky.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.techysky.entity.DataModule;
import com.techysky.entity.Paper;
import com.techysky.entity.Question;
import com.techysky.entity.QuestionList;
import com.techysky.entity.QuestionModel;
import com.techysky.repository.DataModuleRepository;
import com.techysky.repository.QuestionModelRepository;
import com.techysky.repository.QuestionRepository;

@Service
public class QuestionService {
	@Resource
	private QuestionRepository questionRepository;
	@Autowired
	private DataModuleRepository dataModuleRepository;
	@Autowired
	private QuestionModelRepository questionModelRepository;
	@Transactional
	public void save(Question question){
		questionRepository.save(question);
		String q=question.getDataModelId();
		if(q!=null&&q.length()>0){
			for(int i=0;i<q.split(",").length;i++){
				QuestionModel qm=new QuestionModel();
				DataModule dm=dataModuleRepository.findOne(q.split(",")[i]);
				qm.setQuestionId(question.getId());
				qm.setModelId(q.split(",")[i]);
				qm.setModelDmCode(dm.getDmCode());
				questionModelRepository.save(qm);
			}
		}
	}
	@Transactional
	public void delete(String id){
		questionRepository.delete(id);
	}
	
	@Transactional
	public String deleteQuestions(String ids){
		String[]  strs=ids.split(",");
		for(int i=0,len=strs.length;i<len;i++){
		    questionRepository.delete(strs[i]);
		    questionModelRepository.deleteModelByQuestionId(strs[i]);
		}
		return "1";
	}
	public List<QuestionModel> findModelByQuestionId(String questionId){
		return questionModelRepository.findModelByQuestionId(questionId);
	}
	@Transactional
	public void deleteModelByQid(String qid){
		questionModelRepository.deleteModelByQuestionId(qid);
	}
	public Iterable<Question> getAll(){
		return questionRepository.findAll();
	}
	public QuestionList findQuestionByRandom(Paper p){
		QuestionList list=new QuestionList();
		if (p.getRadioCount()!=null) {
			List<Question> radioList=questionRepository.findQuestionByRandom("单选题",p.getRadioCount());
			list.setRadioList(radioList);
		}
		if (p.getCheckCount()!=null) {
			List<Question> checkList=questionRepository.findQuestionByRandom("多选题",p.getCheckCount());
			
			list.setCheckList(checkList);
		}
		if (p.getJudgeCount()!=null) {
			List<Question> judeList=questionRepository.findQuestionByRandom("判断题",p.getJudgeCount());
			list.setJudeList(judeList);
		}
		if (p.getAskCount()!=null) {
			List<Question> askList=questionRepository.findQuestionByRandom("问答题",p.getAskCount());
			list.setAskList(askList);
		}
		if (p.getFillCount()!=null) {
			List<Question> fillList=questionRepository.findQuestionByRandom("填空题",p.getFillCount());
			list.setFillList(fillList);
		}
		return list;
	}
	
	public QuestionList findQuestionByIds(String qids) {
		String[] qidArray=qids.split(",");
		List<Question> radioList = new ArrayList<>();
		List<Question> checkList=new ArrayList<>();
		List<Question> judeList=new ArrayList<>();
		List<Question> askList=new ArrayList<>();
		QuestionList list=new QuestionList();
		for (int i = 0; i < qidArray.length; i++) {
			Question q=questionRepository.findOne(qidArray[i]);
			if ("单选题".endsWith(q.getCode())) {
				radioList.add(q);
			}
			if ("多选题".endsWith(q.getCode())) {
				checkList.add(q);
			}
			if ("判断题".endsWith(q.getCode())) {
				judeList.add(q);
				
			}
			if ("问答题".endsWith(q.getCode())) {
				askList.add(q);
				
			}
		}
		list.setRadioList(radioList);
		list.setCheckList(checkList);
		list.setJudeList(judeList);
		list.setAskList(askList);
		return list;
	}
		public Question findById(String id){
			return questionRepository.findOne(id);
		}
		public List<Question> findQuestionByCodeAndId(String code) {
			
			return questionRepository.findByCode(code);
			
		}
		public Page<Question> findByPage(PageRequest pageable, String search) {
			return questionRepository.findAll((Root<Question> root, CriteriaQuery<?> query, CriteriaBuilder cb)-> {
					List<Predicate> predicates = new ArrayList<>();
					 if (!StringUtils.isEmpty(search)) {
						 
					 Predicate p1=cb.like(root.get("content").as(String.class), "%" + search + "%");
					 Predicate p2=cb.like(root.get("code").as(String.class), "%" + search + "%");
					 predicates.add( cb.or(p1,p2));
					 }
					 query.orderBy(cb.desc(root.get("createDate")));
					 Predicate[] pre = new Predicate[predicates.size()];
					return query.where(predicates.toArray(pre)).getRestriction();
				}
				
			, pageable);
		}
		
		
}
